<?php
if (!defined("_INDEX")) { header("Location: /"); die; }

$friend_id = $route->getByNum(5);

$friend = $db->getRow("select `F`.*,`R`.`user_id` from `friends` as `F`,`reports` as `R` where `F`.`friend_id` = '".$friend_id."' and `F`.`report_id` = `R`.`report_id` limit 1");

if ($friend && $friend['user_id'] == $user->getUserId())
{
	$db->sendQuery("delete from `friends` where `friend_id` = '$friend_id'"); // удаляем запись
	$reports = 0;
	$reports += $db->getCell("select count(`friend_id`) from `friends` where `report_id` = '".$friend['report_id']."'");
	$reports += $db->getCell("select count(`comment_id`) from `comments` where `report_id` = '".$friend['report_id']."'");
	$reports += $db->getCell("select count(`topic_id`) from `topics` where `report_id` = '".$friend['report_id']."'");
	if ($reports == 0) // если больше не осталось записей из отчета report_id, удаляем отчет
		$db->sendQuery("delete from `reports` where `report_id` = '".$friend['report_id']."'");
}

goToPage("/coordinator/reports/look/friends");
?>